<?php
define('FINDPASS_BIND_EMAIL_PORT', 51005); //绑定邮件服务端口
define('FINDPASS_ACTIVATE_EMAIL_PORT', 51008); //激活邮件服务端口
define('FINDPASS_RUBLISH_EMAIL_PORT', 51009); //判断垃圾邮箱服务端口
define('QQ_EMAIL_SERVICE_IP', '192.168.6.130'); //qq邮箱服务相关访问
define('OTHER_EMAIL_SERVICE_IP', '192.168.6.130'); //其他邮箱服务相关访问

function read_buf($fd, $size, $type="PHP_BINARY_READ") {
    while (1) {
        $read   = array($fd);
		$write  = NULL;
		$except = NULL;
		$ret = @socket_select($read, $write, $except, 1);
		if($ret==false) {
			return false;
		}
		else if($ret==0) {
			return false;
		}
		else {
			$buff=socket_read($fd, $size);
			if($buff==false) return false;
			else if($buff=="" || $buff==0) continue;
			return $buff;
		}
    }
}
/*
 * 返回1:垃圾邮箱 0:正常邮箱
 *$ip="192.168.6.65" $port="51009" $email="lihb@163.com" 
 */
function spam_mail($ip, $port, $email) {
	if($ip=="" || $port=="" || $email=="") return 0;

	$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if(!$sock){
		return 0;
	}
	socket_set_nonblock($sock);
	$res=@socket_connect($sock, $ip, $port);
	$ret = socket_select($fd_read = array($sock), $fd_write = array($sock), $except = NULL, 0,500000);
	if($ret === false){
		socket_close($sock);
		return 0;
	}elseif ($ret != 1){
		socket_close($sock);
		return 0;
	}

    socket_set_block($sock);
    @socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
	
	$res=socket_write($sock, $email, strlen($email));
	if($res==false) {
		socket_close($sock);
		return 0;
	}
	$line=read_buf($sock, 8192);
	if($line[0]!='2') {
		socket_close($sock);
		return 0;
	}
	socket_close($sock);

	return 1;
}
/*
 * 返回 1: 成功 0: 失败 
 */
function send_mail($user, $to, $subject, $body) {
	if($user=="" || $to=="") return 0;

	$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if(!$sock){
		return 0;
	}
	$port=FINDPASS_ACTIVATE_EMAIL_PORT;
	if(stristr($to, "@qq.com")) {
		$md5=md5($user);
		$hex=floor(hexdec($md5[0])/4);
		$ip=QQ_EMAIL_SERVICE_IP;
	}
	else $ip=OTHER_EMAIL_SERVICE_IP;
	socket_set_nonblock($sock);
	$res=@socket_connect($sock, $ip, $port);
	$ret = socket_select($fd_read = array($sock), $fd_write = array($sock), $except = NULL, 0,500000);
	if($ret === false){
		socket_close($sock);
		return 0;
	}elseif ($ret != 1){
		socket_close($sock);
		return 0;
	}

    socket_set_block($sock);
    @socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
	
	$mid=uniqid().rand(0, 9).uniqid();
	$date=strftime("%a, %d %b %Y %H:%M:%S")." +0800 (CST)";
	// 发送数据
	//$data="Date: ".$date."\r\n";
	//$data="From: <".$from.">\r\n";
	$data="To: <".$to.">\r\n";
	$data.="Subject: =?GBK?B?".base64_encode($subject)."?=\r\n";
	$data.="Message-Id: <".$mid.">\r\n";
	$data.="X-User: $user\r\n";
	$data.="MIME-Version: 1.0\r\n";
	$data.="X-Mailer: DreamMail 4.4.0.0\r\n";
	//$data.="X-Originating-IP: [114.80.88.48]\r\n";
	$data.="X-Priority: 3\r\n";
	$data.="Content-Type: text/html;\r\n\tcharset=\"GBK\"\r\n";
	$data.="Content-Transfer-Encoding: base64\r\n\r\n";
	$data.=base64_encode($body);
	$data.="\r\n.\r\n";
	$res=socket_write($sock, $data, strlen($data));
	if($res==false) {
		socket_close($sock);
		return 0;
	}
	$line=read_buf($sock, 1024, PHP_NORMAL_READ);
	if($line[0]!='2') {
		socket_close($sock);
		return 0;
	}
	socket_close($sock);

	return 1;
}

//include_once $_SERVER['DOCUMENT_ROOT'] . '/fiveonelib/findpass/bdb_emailbind_midware.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/../../core/public/library/findpass/bdb_emailbind_midware.php';
/**
 * 查询指定email绑定的用户
 * @param string $email
 * @return string or false
 */
function get_bind_user_by_email($email) {
	if(!$result = bdb_emailbind_get_bind_info_by_email($email))
	{
		$result = bdb_emailbind_get_bind_info_by_email($email);
	}
	if (is_array($result) && ($result['user'] != '')) {
		return $result['user'];
	} else {
		return false;
	}
}
?>
